package com.deli.core.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.deli.core.pojo.Brand;
import com.deli.core.pojo.Color;
import com.deli.core.pojo.Product;
import com.deli.core.service.BrandService;
import com.deli.core.service.ColorService;
import com.deli.core.service.ProductService;
import com.deli.core.tools.PageHelper.Page;

/**
 * 商品管理的控制器
 * 
 * @author Administrator
 *
 */
@Controller
public class ProductAction {

	@Autowired
	private ProductService productionService;
	@Autowired
	private BrandService brandService;
	@Autowired
	private ColorService colorService;
	// 通用商品
	private static Logger logger = Logger.getLogger(ProductAction.class);
	@RequestMapping(value = "console/product/{pageName}.do")
	public String consoleProductShow(
			@PathVariable(value = "pageName") String pageName) {
		return "/product/" + pageName;
	}
	@RequestMapping(value = "console/product/list.do")
	public String productShowAll(Model model, String name, Long brandId,
			Integer isShow,Integer pageNum, Integer pageSize) {
		  
		Product product = new Product();
		product.setName(name);
		product.setIsShow(isShow);
		product.setBrandId(brandId);
		List<Brand> listBrands = productionService.findBrandAll();
		Page<Product> pageProducts = productionService.productShowAll(product,pageNum,pageSize);
		model.addAttribute("pageProducts", pageProducts);
		model.addAttribute("name", name);
		model.addAttribute("brands", listBrands);
		model.addAttribute("isShow", isShow);
		model.addAttribute("brandId", brandId);
		logger.debug(listBrands);
		logger.debug(pageProducts);
		return "product/list";
	}
	
	@RequestMapping(value="console/product/showAdd.do")
	public String productShowAdd(Model model,Product product,Brand brand,String color){
		List<Brand> listBrands = productionService.findBrandAll();
		List<Color> listColors = productionService.findColordAll();
		List<Product> listProducts =productionService.productShowAdd(product);
		model.addAttribute("listProducts", listProducts);
		model.addAttribute("listColors", listColors);
		model.addAttribute("listBrands", listBrands);
		return "product/add";
	}
	@RequestMapping(value="console/product/doAdd.do")
	public String productDoAdd(Model model,Product product){
		System.out.println(product);
		productionService.productDoAdd(product);
		return "redirect:/console/product/list.do";
	}
	@RequestMapping(value="console/product/isShow.do")
	public String productIsShow(String ids,Integer flag){
		Product product = new Product();
		product.setIsShow(flag);
		productionService.updatebyisShow(product,ids);
		return "redirect:/console/product/list.do";
	}
}
